package com.sofwin.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.sofwin.pojo.TestPojo;
import com.sofwin.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController extends BaseController {

    @Autowired
    private TestService testService;

    @PostMapping("/list")
    public TableDataInfo list(@RequestBody TestPojo testPojo,
                              @RequestParam(name = "pageCurrent",required = true,defaultValue = "1")Integer pageCurrent,
                              @RequestParam(name = "pageSize",required = true,defaultValue = "5") Integer pageSize){
        LambdaQueryWrapper<TestPojo> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(testPojo.getUsername())){
            wrapper.like(TestPojo::getUsername,testPojo.getUsername());
        }
        if(!StringUtils.isEmpty(testPojo.getEmail())){
            wrapper.eq(TestPojo::getEmail,testPojo.getEmail());
        }
        Page<TestPojo> page = testService.page(new Page<>(pageCurrent, pageSize), wrapper);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody TestPojo testPojo){
        LambdaQueryWrapper<TestPojo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPojo::getId,testPojo.getId());
        TestPojo one = testService.getOne(wrapper);
        if(testPojo.getId() != null&&StringUtils.isNotEmpty(one.toString())){
            testService.updateById(testPojo);
            return AjaxResult.success("修改成功");
        }
        testService.save(testPojo);
        return AjaxResult.success("添加成功");
    }

//    @PostMapping("/addOrUpdate")
//    public AjaxResult addOrUpdateStatus(@RequestBody Status status) {
//        LambdaQueryWrapper<Status> wrapper = new LambdaQueryWrapper<>();
//        if (status.getSId() != null) {
//            wrapper.eq(Status::getSId,status.getSId());
//            Status one = statusService.getOne(wrapper);
//            if (StringUtils.isNotEmpty(one.toString())) {
//                statusService.updateById(status);
//                return AjaxResult.success("修改成功");
//            }
//        }
//        statusService.save(status);
//        return AjaxResult.success("新增成功");
//    }

    @DeleteMapping("/del")
    public AjaxResult del(@RequestBody List<Integer> ids){
        testService.removeByIds(ids);
        return AjaxResult.success("删除成功");
    }
}
